const baseurl= 'https://lanqiu.changgongwang.cn/api/';
const token = uni.getStorageSync('token')
// const token = 'd45294be-9484-420f-8210-b039155e49bd'
console.log(token)
 
function request(options) {
	console.log('Final options:', options);
  return new Promise((resolved, rejected) => {
    uni.request({
      url: baseurl + options.url,
      method: options.method || 'GET',
      data: options.data || {},
      header: options.header || {'Content-Type': 'application/json' },
	  // header: { 'token': token,  'Content-Type': 'application/json' },
      success(res) {
		let resData = res.data;
		if(resData.code == 1 || res.statusCode == 200){
			return resolved(resData);
		}
		if(resData.code == 401 || resData.code == 402){
			uni.removeStorageSync('token')
			uni.reLaunch({
				url:'/pages/myphone/myphone'
			});
			uni.$u.toast(resData.msg)
			return rejected(res);
		}else {
			uni.$u.toast(resData.msg)
			return rejected(res);
		} 
      },
      fail(err) {
        rejected(err); // 请求失败时返回错误信息
      }
    });
  });
}

// 创建请求工具类或对象
export default {
  get(url, options = {}) {
    return request({ url, method: 'GET', ...options });
  },
  post(url, options = {}) {
    return request({ url, method: 'POST', ...options });
  },
  put(url, options = {}) {
    return request({ url, method: 'PUT', ...options });
  },
  delete(url, options = {}) {
    return request({ url, method: 'DELETE',...options});
  }
};